package com.own.component.common.base.entity.po;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.own.component.common.login.user.BaseLoginUser;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;

import java.io.Serializable;

/**
 * BasePo 基础信息po
 *
 * @author chenxueli
 * @date 2022-09-10 12:01:52
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
public abstract class BaseSortPo extends BasePo implements Serializable {

    @Schema(title = "顺序，默认数字越小约在前面")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @TableField
    private Integer sortOrder;

    /**
     * 设置初始化信息
     *
     * @param loginUser 登录用户对象
     */
    @Override
    public void init(BaseLoginUser loginUser) {
        super.init(loginUser);
        if (this.sortOrder == null) {
            this.sortOrder = 0;
        }
    }

    /**
     * 是否在末尾
     *
     * @param maxSort 系统里的排序
     * @return 是否在末尾
     */
    public boolean inLast(int maxSort) {
        if (maxSort == -1) {
            sortOrder = 0;
            return true;
        }
        if (sortOrder == null || sortOrder < 0) {
            sortOrder = 0;
        }
        if (sortOrder > maxSort) {
            sortOrder = maxSort + 1;
            return true;
        } else {
            return false;
        }
    }

}
